﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using WiMo.Games.Drawables;
using WiMo.Games.Drawing.Silverlight;

namespace WiMo.Games.Drawing.WP7S.Drawables
{
    public class SpriteText : SpriteTextBase
    {
        private TextBlock _textBlock;

        public static SpriteText Create(PointF position, Vector2 scale, string text, ISpriteFont font, Color foreColor, Origin origin)
        {
            var spriteText = new SpriteText();
            spriteText.Scale = scale;
            spriteText.Font = font;
            spriteText.Position = position;
            spriteText.Text = text;
            spriteText.ForeColor = foreColor;
            spriteText.IsTransient = true;

            var textBlock = new TextBlock();
            textBlock.TextAlignment = TextAlignment.Left;
            textBlock.HorizontalAlignment = HorizontalAlignment.Left;
            textBlock.Text = text;
            textBlock.Foreground = new System.Windows.Media.SolidColorBrush(foreColor.GetMediaColor());

            TransformGroup tg = new TransformGroup();
            var translate = new TranslateTransform();
            var rotate = new RotateTransform();
            var scaleTransform = new ScaleTransform();
            scaleTransform.ScaleX = scale.X;
            scaleTransform.ScaleY = scale.Y;

            tg.Children.Add(translate);
            tg.Children.Add(rotate);
            tg.Children.Add(scaleTransform);

            textBlock.RenderTransform = tg;

            if (origin == Origin.Center)
            {
                textBlock.SetValue(Canvas.TopProperty, (double)spriteText.Position.Y - textBlock.ActualHeight * scale.Y / 2);
                textBlock.SetValue(Canvas.LeftProperty, (double)spriteText.Position.X - textBlock.ActualWidth * scale.X / 2);
            }
            else
            {
                textBlock.SetValue(Canvas.TopProperty, (double)spriteText.Position.Y);
                textBlock.SetValue(Canvas.LeftProperty, (double)spriteText.Position.X);
            }

            spriteText._textBlock = textBlock;

            return spriteText;
        }

        private TextBlock CreateTextBlock()
        {
            var textBlock = new TextBlock();
            textBlock.TextAlignment = TextAlignment.Left;
            textBlock.HorizontalAlignment = HorizontalAlignment.Left;
            textBlock.Text = Text;
            textBlock.Foreground = new System.Windows.Media.SolidColorBrush(ForeColor.GetMediaColor());

            TransformGroup tg = new TransformGroup();
            var translate = new TranslateTransform();
            var rotate = new RotateTransform();
            var scaleTransform = new ScaleTransform();
            scaleTransform.ScaleX = Scale.X;
            scaleTransform.ScaleY = Scale.Y;

            tg.Children.Add(translate);
            tg.Children.Add(rotate);
            tg.Children.Add(scaleTransform);

            textBlock.RenderTransform = tg;

            if (Origin == Origin.Center)
            {
                textBlock.SetValue(Canvas.TopProperty, (double)Position.Y - textBlock.ActualHeight * Scale.Y / 2);
                textBlock.SetValue(Canvas.LeftProperty, (double)Position.X - textBlock.ActualWidth * Scale.X / 2);
            }
            else
            {
                textBlock.SetValue(Canvas.TopProperty, (double)Position.Y);
                textBlock.SetValue(Canvas.LeftProperty, (double)Position.X);
            }

            return textBlock;
        }

        public override object DrawInformation
        {
            get
            {
                if (_textBlock == null || IsDirty)
                    _textBlock = CreateTextBlock();

                return _textBlock;
            }
            set
            {
                if(value != null)
                    throw new NotSupportedException("can not set the draw information to anything other than null, it will be set to a TextBlock automatically");
                else
                    _textBlock = null;
            }
        }

        public override void Render(IDisplay display)
        {
            display.Render(this);
        }
    }
}
